package team.dmh.wx.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: Hz1kai
 * @Date: 2018/10/20 19:52
 * @Description: 继承ChannelInboundHandlerAdapter，重写userEventTriggered方法用于心跳检测
 */
@Slf4j
public class HeartBeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleStateEvent.READER_IDLE_STATE_EVENT.state()) {
//                log.info("channel into read idle");
            }else if (event.state()==IdleStateEvent.WRITER_IDLE_STATE_EVENT.state()){
//                log.info("channel into write idle");
            }else if (event.state()==IdleStateEvent.ALL_IDLE_STATE_EVENT.state()){
                Channel channel=ctx.channel();
                channel.close();
                log.info("current clients size is "+ChatHandler.clients.size());
            }
        }
    }
}
